package com.xsd.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Description: FileChannel案例 2
 * @Author: zhoufuzong
 * @Created: 2021-01-21 22:20:46
 * @Version: 1.1.0
 */

public class NIOFileChannel02 {
    public static void main(String[] args) throws IOException {

        File file = new File("file01.txt");
        FileInputStream fis = new FileInputStream(file);

        FileChannel channel = fis.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());

        channel.read(byteBuffer);

        // byteBuffer的字节数据转成String
        System.out.println(new String(byteBuffer.array()));
        fis.close();

    }
}
